using System.Collections.Generic;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE.Skill
{
    //召唤物
    public class Summon : SkillBullet
    {
        private UnitBase target;
        private Animator animator;
        private int state = 0;//0 跑  1 执行动作
        private float stateStartTime;
        private string actionName;
        private bool hasHit = false;
        private List<SummonAction> actions;
        private SummonAction currentAction;

        private class SummonAction
        {
            //public int type;//0 跑 1 原地攻击
            public int index;
            public string actionName;
            public List<Vector3> actionList;//x 帧  y 元素
            public float duration;
            public float startTime;
        }


        public void SetTarget(UnitBase target)
        {
            this.target = target;
        }

        public override void Initialize(PveSkillBulletConfig config, PveSkillConfig skillConfig,SkillBase skill, Vector3 startPosition, Vector3 offset, Vector3 angle,
            UnitBase source)
        {
            base.Initialize(config, skillConfig,skill, startPosition, offset, angle, source);
            animator = bullet.GetComponentInChildren<Animator>();
            //Play("SkillRun");
            bullet.transform.position = startPosition;
            lastPosition = startPosition;
            if (actions == null)
            {
                actions = new List<SummonAction>();
                actions.Add(new SummonAction
                {
                    index = 0,
                    actionName = "SkillAttack",
                    actionList = new List<Vector3>
                    {
                        new Vector3(10,config.elements[0]),
                    },
                    duration = 1.3f,
                });
                actions.Add(new SummonAction
                {
                    index = 1,
                    actionName = "SkillJumpAttack",
                    actionList = new List<Vector3>
                    {
                        new Vector3(13,config.elements[0]),
                    },
                    duration = 1.4f,
                });
                actions.Add(new SummonAction
                {
                    index = 2,
                    actionName = "SkillDead",
                    actionList = new List<Vector3>
                    {
                        
                    },
                    duration = 2f,
                });
            }

            currentAction = null;
            for (int i = 0; i < actions.Count; i++)
            {
                actions[i].startTime = 0;
                for (int j = 0; j < actions[i].actionList.Count; j++)
                {
                    actions[i].actionList[j] = new Vector3(actions[i].actionList[j].x,actions[i].actionList[j].y,0);
                }
            }
            state = 0;
        }

        private void Play(string actionName)
        {
            if (!string.IsNullOrEmpty(this.actionName))
            {
                animator.ResetTrigger(this.actionName);
            }
            animator.SetTrigger(actionName);
            this.actionName = actionName;
        }

        private void CheckNextAction()
        {
            int index = 0;
            if (currentAction == null)
            {
                index = 0;
            }
            else
            {
                index = currentAction.index + 1;
            }

            if (index >= actions.Count)
            {
                Dispose();
            }
            else
            {
                currentAction = actions[index];
                currentAction.startTime = TimeManager.logicTime;
                Play(currentAction.actionName);
            }
            
        }

        private void UpdateAction()
        {
            if (currentAction != null)
            {
                if (TimeManager.logicTime - currentAction.startTime > currentAction.duration)
                {
                    state = 0;
                    return;
                }

                for (int i = 0; i < currentAction.actionList.Count; i++)
                {
                    if (TimeManager.logicTime - currentAction.startTime >= currentAction.actionList[i].x / 30f && currentAction.actionList[i].z == 0)
                    {
                        currentAction.actionList[i] = new Vector3(currentAction.actionList[i].x,currentAction.actionList[i].y,1);
                        HitUnit((BattleUnit) target);
                    }
                }
            }
        }
        
        public override void Update(float deltaTime)
        {
            if (IsDisposed)
            {
                return;
            }

            if (state == 0)
            {
                var diff = Vector3.Distance(target.Data.position, lastPosition);
                if (diff < 1)
                {
                    state = 1;
                    CheckNextAction();
                }
                else
                {
                    lastPosition += config.speed * deltaTime * (target.Data.position - lastPosition).normalized;
                    Play("SkillRun");
                }

                if (bullet != null)
                {
                    bullet.transform.position = lastPosition;
                }
            }
            else if (state == 1)
            {
                UpdateAction();
            }
        }

        protected override void UpdatePosition(float deltaTime)
        {
            //base.UpdatePosition(deltaTime);
        }
    }
}